package com.chenchao.net;

import java.io.IOException;
import java.net.*;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/25 11:14
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test06S.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * UDP客户端（也可以是服务端）
 */
public class Test06C {
    public static void main(String[] args) throws IOException {
        /*新建端口*/
        DatagramSocket datagramSocket = new DatagramSocket(9090);

        /*新建数据包，把数据和发送者信息一并打包*/
        String str="我是UDP发送者";
        InetAddress localhost = InetAddress.getByName("localhost");
        DatagramPacket datagramPacket = new DatagramPacket
                (str.getBytes(),str.getBytes().length,localhost,8080);

        /*发送数据包*/
        datagramSocket.send(datagramPacket);


        /*关闭资源*/
        datagramSocket.close();
    }
}